<?php
namespace app\index\controller;

use app\common\controller\Base;
use app\index\model\Item;

/**
 * 道具
 */
class Pack extends Base
{

    use \app\index\controller\traits\User;
    use \app\index\controller\traits\Xml;

    //道具属性
    private $_prop_type  = ["1" => "装备", "2" => "技能书", "3" => "消耗品", "4" => "其它", "5" => "符文"];
    private $_main_attr  = ["1" => "攻击", "2" => "护甲", "3" => "血量", "4" => "速度", "5" => "智慧"];
    private $_extra_attr = ["1" => "攻击", "2" => "护甲", "3" => "血量", "4" => "速度", "5" => "智慧额外", "6" => "暴击率", "7" => "韧性", "8" => "破甲值", "9" => "命中率", "10" => "闪避率", "11" => "暴击倍率"];

    /**
     * 表字段初始化
     */
    public function _initialize()
    {

        //基类授权
        //
        parent::_initialize();

        //道具表查询

        $this->prop_path    = "./static/xml/acc_dragon_prop.xml";
        $this->attr_path    = "./static/xml/acc_monster_attr.xml";
        $this->quality_path = "./static/xml/acc_monster_quality.xml";

        $this->prop    = $this->getArrFromXml($this->prop_path, 'id', 'name');
        $this->attr    = $this->getArrFromXml($this->attr_path, 'id', 'description');
        $this->quality = $this->getArrFromXml($this->quality_path, 'id', 'name');
    }
    /**
     * 基本信息查询
     */
    public function queryBasicInfo()
    {

        $id       = array_key_exists("id", $_REQUEST) ? $_REQUEST['id'] : null;
        $nickname = array_key_exists("nickname", $_REQUEST) ? $_REQUEST['nickname'] : null;

        $id = $this->getUserId($id, $nickname);

        if (empty($id)) {
            _callback(array('success' => true, 'msg' => "提示:没有找到玩家信息", 'type' => "user-not-exist"));
        }

        //查询玩家佣兵
        $res = $this->queryItemsById($id);

        if ($res) {
            //字段转换
            $res = $this->transItemsById($res);

            $return['rows']    = $res;
            $return['total']   = count($res);
            $return['success'] = true;
        } else {
            $return['rows']    = array();
            $return['total']   = 0;
            $return['success'] = true;
        }

        _callback($return);
    }
    /**
     * 道具信息查询
     */
    public function queryItemsById($id)
    {

        $query = Item::field("`id`,`cid`,`num`,`type`,`quality`,`main_attri`,`extra_attri`,`special_attri`")
            ->where("`characterid`='{$id}'")
            ->order("id asc")
            ->select();

        if (!$query) {
            return [];
        }

        $rows = [];

        foreach ($query as $data) {
            array_push($rows, $data->getData());
        }
        return $rows;
    }

    /**
     * 数据转换
     */
    public function transItemsById($recs)
    {

        $prop    = $this->prop;
        $attr    = $this->attr;
        $quality = $this->quality;

        if (!empty($recs)) {

            foreach ($recs as $k => $v) {

                //道具ID转名称
                $recs[$k]['cid'] = array_key_exists($v['cid'], $prop) ? $prop[$v['cid']] : $v['cid'];

                //品质转名称
                $recs[$k]['quality'] = array_key_exists($v['quality'], $quality) ? $quality[$v['quality']] : $v['quality'];

                //类型转名称
                $type             = $this->_prop_type;
                $recs[$k]['type'] = array_key_exists($v['type'], $type) ? $type[$v['type']] : $v['type'];

                $temp = "";

                $_main_attr  = $this->_main_attr;
                $_extra_attr = $this->_extra_attr;

                //主属性
                if (!empty($v['main_attri'])) {
                    $main = explode(";", $v['main_attri']);
                    foreach ($main as $ks => $vs) {

                        $item = explode("=", $vs);

                        if (!empty($item)) {

                            $_temp = array_key_exists($item[0], $_main_attr) ? $_main_attr[$item[0]] : $item[0];
                            $_temp .= "={$item[1]};";

                            $temp .= $_temp;
                        }
                    }
                    $temp .= " ";
                }

                //额外属性
                if (!empty($v['extra_attri'])) {
                    $temp .= "| ";
                    $extra = explode(";", $v['extra_attri']);
                    foreach ($extra as $ko => $vo) {

                        $item = explode("=", $vo);

                        if (!empty($item)) {

                            $_temp = array_key_exists($item[0], $_extra_attr) ? $_extra_attr[$item[0]] : $item[0];
                            $_temp .= "={$item[1]};";

                            $temp .= $_temp;
                        }
                    }
                }
                $recs[$k]['attribute'] = $temp;

                //特殊属性
                if (!empty($v['special_attri'])) {
                    $recs[$k]['special_attri'] = array_key_exists($v['special_attri'], $attr) ? $attr[$v['special_attri']] : $v['special_attri'];
                } else {
                    $recs[$k]['special_attri'] = "";
                }
            }
        }
        return ($recs);
    }
}
